package com.dawnling.androidutils.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 百分比进度条
 */
public class ProgressView extends View {

    private Context mContext;

    /**
     * 分段颜色
     */
    private static int[] SECTION_COLORS = new int[]{Color.GREEN, Color.RED};

    /**
     * 默认宽度
     */
    private int mWidth = 500;

    /**
     * 默认高度
     */
    private int mHeight = 90;

    /**
     * 设置温度的最大范围
     */
    private float maxCount = 100f;

    /**
     * 设置当前温度
     */
    private float currentCount = 50f;

    /**
     * 当前刻度位置
     */
    private float selction;

    /**
     * 主画笔，画刻度尺
     */
    private Paint mPaint;
    private Paint mPaintRight;

    /**
     * 文字画笔
     */
    private Paint textPaint;

    /**
     * 画圆柱
     */
    private RectF rectProgressLeft;
    private RectF rectProgressRight;
    private RectF rectProgressCenter;
    private LinearGradient shader;

    /**
     * 圆角矩形的高度
     */
    private int mDefaultTempHeight = dipToPx(20);
    /**
     * 默认字体大小
     */
    private int mDefaultTextSize = 30;
    private int textSpace = dipToPx(5);
    private float centerWhite = 117f;//中间圆柱默认宽度
    private float leftSetion = 0f;//比例刻度位置
    private float leftTextSetion = 0f;//比例刻度 文字位置

    public ProgressView(Context context) {
        super(context);
        init(context);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化各种画笔
     * @param context
     */
    private void init(Context context) {
        this.mContext = context;
        //圆角矩形paint
        mPaint = new Paint();
        //防止边缘的锯齿
        mPaint.setAntiAlias(true);
        //圆角矩形paint
        mPaintRight = new Paint();
        //防止边缘的锯齿
        mPaintRight.setAntiAlias(true);

        //文本paint
        textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(mDefaultTextSize);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setColor(mContext.getResources().getColor(android.R.color.white));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mWidth, mHeight);
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(mWidth, heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, mHeight);
        }else setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //左边宽
        int leftWhite = (int)((currentCount/100) * mWidth);
        //右边宽
        int rightWhite = mWidth - (int)((currentCount/100) * mWidth);
        final int paddingLeft = getPaddingLeft();
        final int paddingBottom = getPaddingBottom();

        //确定圆角矩形的范围,在TmepView的最底部,top位置为总高度-圆角矩形的高度
        rectProgressLeft = new RectF();
        rectProgressLeft.left = 0 + paddingLeft;
        rectProgressLeft.top = mHeight/2 - mDefaultTempHeight/2;
        rectProgressLeft.right = leftWhite;
        rectProgressLeft.bottom = mHeight/2 + mDefaultTempHeight/2 - paddingBottom;
        //确定圆角矩形的范围,在TmepView的最底部,top位置为总高度-圆角矩形的高度
        rectProgressRight = new RectF();
        rectProgressRight.left = leftWhite;
        rectProgressRight.top = mHeight/2 - mDefaultTempHeight/2;
        rectProgressRight.right = leftWhite + rightWhite;
        rectProgressRight.bottom = mHeight/2 + mDefaultTempHeight/2 - paddingBottom;

        shader = new LinearGradient(0, mHeight - mDefaultTempHeight, leftWhite, mHeight, SECTION_COLORS, null, Shader.TileMode.MIRROR);
        mPaint.setShader(shader);
        mPaintRight.setColor(mContext.getResources().getColor(android.R.color.darker_gray));

        //当前位置占比
        selction = currentCount / maxCount;
        if (leftWhite != 0 && rightWhite != 0){
            //绘制圆角矩形 mDefaultTempHeight / 2确定圆角的圆心位置
            canvas.drawRoundRect(rectProgressLeft, mDefaultTempHeight / 2, mDefaultTempHeight / 2, mPaint);
            canvas.drawRoundRect(rectProgressRight, mDefaultTempHeight / 2, mDefaultTempHeight / 2, mPaintRight);
            leftSetion = mWidth * selction - centerWhite/2;
            leftTextSetion = mWidth * selction;
        }else if (leftWhite == 0){// 0%
            canvas.drawRoundRect(rectProgressRight, mDefaultTempHeight / 2, mDefaultTempHeight / 2, mPaintRight);
            leftSetion = 0;
            leftTextSetion = centerWhite/2;
        }else if (rightWhite == 0){// 100%
            canvas.drawRoundRect(rectProgressLeft, mDefaultTempHeight / 2, mDefaultTempHeight / 2, mPaint);
            leftSetion = mWidth - centerWhite;
            leftTextSetion = leftWhite - centerWhite/2;
        }
        //中间圆柱百分比背景
        rectProgressCenter = new RectF();
        rectProgressCenter.left = leftSetion;
        rectProgressCenter.top = 0;
        rectProgressCenter.right = leftSetion + centerWhite;
        rectProgressCenter.bottom = mHeight;
        canvas.drawRoundRect(rectProgressCenter, mHeight/2, mHeight/2, mPaint);
        //绘制文本
        String text = (int) currentCount + " %";
        //确定文本的位置 x=tempViwe的宽度*当前位置占比 y=tempView的高度-圆角矩形的高度-三角形的高度-文本的间隙  原本文字位置 进度条之上：mHeight - mDefaultTempHeight - textSpace
        canvas.drawText(text, leftTextSetion, mHeight/2 + mDefaultTempHeight/2 - textSpace/2, textPaint);
    }

    /**
     * 单位转换
     * @param dip
     * @return
     */
    private int dipToPx(int dip) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    /***
     * 设置最大的温度值
     * @param maxCount
     */
    public void setMaxCount(float maxCount) {
        this.maxCount = maxCount;
    }

    /***
     * 设置当前的温度
     * @param currentCount
     */
    public void setCurrentCount(float currentCount) {
        if (currentCount > maxCount) {
            this.currentCount = maxCount - 5;
        } else if (currentCount < 0f) {
            currentCount = 0f + 5;
        } else {
            this.currentCount = currentCount;
        }
        invalidate();
    }

    /**
     * 设置刻度
     */
    public void setTempHeight(int height) {
        this.mDefaultTempHeight = height;
    }

    /**
     * 设置整个高度
     */
    public void setHeight(int height) {
        this.mHeight = height;
    }

    /**
     * 设置中间进度文字背景图
     */
    public void setSetionColor(int[] setionColor) {
        this.SECTION_COLORS = setionColor;
    }

    /**
     * 设置文字大小
     * @param textSize
     */
    public void setTextSize(int textSize) {
        this.mDefaultTextSize = textSize;
    }

    /**
     * 获取温度计最大刻度
     * @return
     */
    public float getMaxCount() {
        return maxCount;
    }

    /**
     * 获取当前刻度
     * @return
     */
    public float getCurrentCount() {
        return currentCount;
    }
}